package com.alasbusy.alasbusyapp.base;

import android.Manifest;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;
import com.alasbusy.alasbusyapp.R;
import com.tbruyelle.rxpermissions.RxPermissions;
import rx.functions.Action1;


/**
 * Created by kyc on 2017/5/2.
 */
public abstract class BaseActivity extends BasePhotoActivity{

    private View mBack;
    private TextView mTitle;
    private View mTitle_image;
    private TextView title_right_txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());

        RxPermissions rxPermissions = new RxPermissions(this);
        rxPermissions.request(
                Manifest.permission.SYSTEM_ALERT_WINDOW,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.RECORD_AUDIO,
                //Manifest.permission.BLUETOOTH_ADMIN,
                //Manifest.permission.BLUETOOTH,
                Manifest.permission.MOUNT_UNMOUNT_FILESYSTEMS,
                //Manifest.permission.CAMERA,
                Manifest.permission.INTERNET,
                Manifest.permission.ACCESS_NETWORK_STATE,
                Manifest.permission.ACCESS_WIFI_STATE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Action1<Boolean>() {
                    @Override
                    public void call(Boolean granted) {
                        if (granted) {

                        } else {

                        }
                    }
                });
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.INTERNET}, 200);
        initView();
        initData();
        initListener();
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,  String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void getVisibility(boolean visi, final Activity activity){
        if(visi) {
            mBack.setVisibility(View.VISIBLE);
            mBack.setEnabled(true);
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    activity.finish();
                }
            });
        }else {
            mBack.setVisibility(View.INVISIBLE);
            mBack.setEnabled(false);
        }
    }

    public void setTop(View view){
        mBack = view.findViewById(R.id.back);
        mTitle = (TextView) view.findViewById(R.id.title);
        mTitle_image = view.findViewById(R.id.title_image);
        title_right_txt = (TextView) view.findViewById(R.id.title_right_txt);
    }

    public void rightVisibility(String str, int img){
        if(!TextUtils.isEmpty(str)){
            title_right_txt.setText(str);
            title_right_txt.setVisibility(View.VISIBLE);
            mTitle_image.setVisibility(View.GONE);
        }else if(img != -1){
            mTitle_image.setBackgroundResource(img);
            mTitle_image.setVisibility(View.VISIBLE);
            title_right_txt.setVisibility(View.GONE);
        }else {
            title_right_txt.setVisibility(View.INVISIBLE);
            mTitle_image.setVisibility(View.GONE);
        }
    }

    public View getImageView(){
        return mTitle_image;
    }

    public void getTitle(String title){
        mTitle.setText(title);
    }

    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config=new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config,res.getDisplayMetrics() );
        return res;
    }

    /*
    初始化控件
     */
    protected abstract void initView();

    /*
    初始化数据
     */
    protected abstract void initData();

    /*
    设置监听事件
     */
    protected abstract void initListener();

    /*
    加载布局
     */
    public abstract int getLayout();

    public void ToastWarning(String str) {
        //Toasty.warning(this, str, Toast.LENGTH_SHORT).show();
    }

    public void ToastSuccess(String str){
       // Toasty.success(this, str, Toast.LENGTH_SHORT).show();
    }
}
